package com.lzh.seatrend.service.impl;

import com.lzh.seatrend.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Classname RedisServiceImpl
 * @Description TODO
 * @Date 2021/11/15 15:51
 * @Created by liuzihao
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 过期时间
     */
    private long phoneExpire = 1;

    private int SAVE = 1;
    private int NOT_SAVE = 0;

    @Override
    @Cacheable
    public int saveCodeToRedis(String phone, String code) {
        redisTemplate
                .opsForValue()
                .set(phone,code,phoneExpire, TimeUnit.MINUTES);
        return redisTemplate.hasKey(phone) ? SAVE : NOT_SAVE;
    }

}
